package com.xiaoge.libraryuse.activity;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.AbsListView;
import android.widget.ListView;

import com.xiaoge.basedevlibrary.base.GuoBaseActivity;
import com.xiaoge.libraryuse.R;
import com.xiaoge.libraryuse.adapter.AdapterVideoList;

import cn.jzvd.JZVideoPlayer;

public class VideoListActivity extends GuoBaseActivity {

    ListView listView;
    SensorManager sensorManager;
    JZVideoPlayer.JZAutoFullscreenListener sensorEventListener;
    private AdapterVideoList mVideoListAdapter;
    /**
     * 当前列表第一个可见位置
     **/
    private int firstVisiblePosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);

        listView = (ListView) findViewById(R.id.listview);
        mVideoListAdapter = new AdapterVideoList(this,
                VideoConstant.videoUrls[0],
                VideoConstant.videoTitles[0],
                VideoConstant.videoThumbs[0]);
        listView.setAdapter(mVideoListAdapter);

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            // 上次停留的位置
            int lastPosition = -1;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (SCROLL_STATE_TOUCH_SCROLL == scrollState) { //列表触摸滑动状态

                } else if (SCROLL_STATE_FLING == scrollState) {//列表停止触摸惯性滑动状态

                } else if (SCROLL_STATE_IDLE == scrollState) {//列表滑动停止状态
                    // 停止滑动的时候 播放视频列表中显示的第一个视频
                    if (lastPosition == firstVisiblePosition) {

                    } else {
                        lastPosition = view.getFirstVisiblePosition();
                        // 播放之前释放掉所有的视频播放
                        JZVideoPlayer.releaseAllVideos();
                        /*JZVideoPlayerStandard itemVideoPlayer = (JZVideoPlayerStandard) view.getChildAt(1).findViewById(R.id.videoplayer);
                        itemVideoPlayer.startVideo();*/

                        mVideoListAdapter.startPlayAt(firstVisiblePosition + 1);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                // 赋值第一个可见位置的值
                firstVisiblePosition = view.getFirstVisiblePosition();
                // System.out.println(firstVisiblePosition + "===" + lastPosition);
                // 播放之前释放掉所有的视频播放
                JZVideoPlayer.onScrollReleaseAllVideos(view, firstVisibleItem, visibleItemCount, totalItemCount);
            }
        });
        // 根据屏幕方向自动设置全屏
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorEventListener = new JZVideoPlayer.JZAutoFullscreenListener();
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(sensorEventListener);
        JZVideoPlayer.releaseAllVideos();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
